<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择状态">
          <el-option v-for="dict in dict.type.moe_acceptance_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-has-permi="['system:acceptance:export']" icon="el-icon-download" plain size="mini" type="warning"
          @click="handleExport">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="acceptanceList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="验收评价项ID" prop="id" />
      <el-table-column align="center" label="项目所属" prop="projectName">
      </el-table-column>
      <!-- 验证时间 -->
      <el-table-column align="center" label="验收时间" prop="acceptanceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acceptanceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="验收专家" prop="acceptanceExpert" />
      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.moe_acceptance_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" prop="remark" />
      <el-table-column align="center" label="详情" prop="remark">
        <template slot-scope="scope">
          <el-button v-has-permi="['system:acceptance:query']" icon="el-icon-view" size="mini" type="text"
            @click="handleAcceptanceDetails(scope.row)">详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="验收" prop="status">
        <template slot-scope="scope">
          <el-row style="display: flex; justify-content: space-evenly;">
            <div title="验收">
              <el-button v-has-permi="['system:acceptance:accept']" circle icon="el-icon-check" size="mini" type="success"
                @click="handleBusiness(scope.row, '验收')" />
            </div>
            <div title="驳回">
              <el-button v-has-permi="['system:acceptance:reject']" circle icon="el-icon-refresh-left" size="mini"
                type="danger" @click="handleBusiness(scope.row, '驳回')" />
            </div>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
      @pagination="getList" />

    <!-- 添加或修改验收管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目所属" prop="projectId">
          <el-input v-model="form.projectName" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="验收专家" prop="acceptanceExpert" v-if="this.title === '验收'">
          <el-input v-model="form.acceptanceExpert" placeholder="请输入验收专家" />
        </el-form-item>
        <el-form-item label="验收意见" prop="acceptanceOpinions" v-if="this.title === '验收'">
          <el-input v-model="form.acceptanceOpinions" placeholder="请输入验收意见" />
        </el-form-item>
        <el-form-item label="驳回意见" prop="acceptanceOpinions" v-if="this.title === '驳回'">
          <el-input v-model="form.acceptanceOpinions" placeholder="请输入驳回意见" />
        </el-form-item>
        <el-form-item label="评分" prop="grade" v-if="this.title === '验收'">
          <el-rate v-model="form.grade" :colors="colors">
          </el-rate>
        </el-form-item>
        <el-form-item label="附件" prop="file">
          <file-upload v-model="form.file" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入内容" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 验收条目明细 -->
    <el-dialog :title="title" :visible.sync="openAcceptanceDetails" append-to-body width="500px">
      <el-form ref="form" :model="list" :rules="rules" label-width="80px">
        <el-form-item label="项目类型" prop="projectId">
          <el-input v-model="list.category" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectId">
          <el-input v-model="list.projectName" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="合同" prop="projectId">
          <el-input v-model="list.contractualName" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="projectId">
          <el-input v-model="list.startTime" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="结束时间" prop="projectId">
          <el-input v-model="list.endTime" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="项目级别" prop="projectId">
          <el-input v-model="list.level" :disabled="true">
          </el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="projectId">
          <el-input v-model="list.nickName" :disabled="true">
          </el-input>
        </el-form-item>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addAcceptance, delAcceptance, getAcceptance, listAcceptance, updateAcceptance, getInfo } from "@/api/system/acceptance";

export default {
  name: "Acceptance",
  dicts: ['moe_acceptance_status', 'sys_user_sex'],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 验收管理表格数据
      acceptanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      list: '',
      // 是否显示弹出层
      openAcceptanceDetails: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        acceptanceExpert: [
          { required: true, message: "验收专家不能为空", trigger: "change" }
        ],
        grade: [
          { required: true, message: "评分不能为空", trigger: "change" }
        ],
      },
      // 评分颜色
      colors: ['#99A9BF', '#F7BA2A', '#FF9900']
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询验收管理列表 */
    getList () {
      this.loading = true;
      listAcceptance(this.queryParams).then(response => {
        this.acceptanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        projectId: null,
        projectName: null,
        acceptanceTime: null,
        acceptanceExpert: null,
        acceptanceOpinions: null,
        grade: null,
        file: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 获取验收详情*/
    handleAcceptanceDetails (row) {
      if (row.status === "1" || row.status === "2") {
        this.$message('该项目待验收或已驳回，暂无法查询详情')
        return
      }
      const id = row.id || this.ids
      getInfo(id).then(response => {
        // this.form = response.data;
        console.log(response);
        this.list = response.data
        // console.log(this.list);
        this.openAcceptanceDetails = true
        this.title = "项目详情";
      });
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加验收";
    },
    /** 驳回操作 */
    handleBusiness (row, title) {
      if (row.status === "0" || row.status === "2") {
        this.$message('该项目已验收或已驳回')
        return
      }
      this.reset();
      const id = row.id || this.ids
      getAcceptance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = title;
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            let message;
            if (this.title === '验收') {
              this.form.status = "0"
              message = '已验收';
            } else if (this.title === '驳回') {
              this.form.status = "2"
              message = '已驳回';
            }
            updateAcceptance(this.form).then(response => {
              this.$modal.msgSuccess(message);
              this.open = false;
              this.getList();
            });
          } else {
            addAcceptance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除验收管理编号为"' + ids + '"的数据项？').then(function () {
        return delAcceptance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('system/acceptance/export', {
        ...this.queryParams
      }, `acceptance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
